package com.mjlf.hometeach.common.db.provider.address;

import com.mjlf.hometeach.common.entity.address.District;
import org.apache.ibatis.jdbc.SQL;

/**
 * @auther a123
 * @create 2018-05-03 23:07
 * @desc 地区动态SQL类
 */
public class DistrictProvider {

    /**
     * 添加地区信息sql
     * @param distric
     * @return
     */
    public String save(District distric){
        SQL sql = new SQL();
        if(distric != null){
            sql.INSERT_INTO("district_tbl");

            sql.VALUES("cn_name", "#{cnName}");
            sql.VALUES("parent_id", "#{parentId}");
            sql.VALUES("initial", "#{initial}");
            sql.VALUES("initials", "#{initials}");
            sql.VALUES("pinyin", "#{pinyin}");
            sql.VALUES("extra", "#{extra}");
            sql.VALUES("suffix", "#{suffix}");
            sql.VALUES("code", "#{code}");
            sql.VALUES("area_code", "#{areaCode}");
            sql.VALUES("level", "#{level}");
            sql.VALUES("create_time", "#{createTime}");
            sql.VALUES("modify_time", "#{modifyTime}");
        }
        return sql.toString();
    }

    /**
     * 根据id删除指定信息
     * @return
     */
    public String deleteById(){
        SQL sql = new SQL();
        sql.DELETE_FROM("district_tbl");

        sql.WHERE("id = #{id}");

        return sql.toString();
    }

    /**
     * 根据id动态判断地区属性是否为null，不则更新该字段属性
     * @param distric
     * @return
     */
    public String updateById(District distric){
        SQL sql = new SQL();
        sql.UPDATE("district_tbl");
        if(distric != null){
            if(distric.getCnName() != null){
                sql.SET("cn_name = #{cnName}");
            }
            if(distric.getParentId() != null){
                sql.SET("parent_id = #{parentId}");
            }
            if(distric.getInitial() != null){
                sql.SET("initial=#{initial}");
            }
            if(distric.getInitials() != null){
                sql.SET("initials = #{initials}");
            }
            if(distric.getPinyin() != null){
                sql.SET("pinyin = #{pinyin}");
            }
            if(distric.getExtra() != null){
                sql.SET("extra = #{extra}");
            }
            if(distric.getSuffix() != null){
                sql.SET("suffix = #{suffix}");
            }
            if(distric.getCode() != null){
                sql.SET("code = #{code}");
            }
            if(distric.getLevel() != null){
                sql.SET("level = #{level}");
            }
            sql.SET("modify_time = " + System.currentTimeMillis());
            sql.WHERE("id = #{id}");
        }

        return sql.toString();
    }

    /**
     * 根据动态添加查询地区集合
     * @param distric
     * @return
     */
    public String listByCondition(District distric){
        SQL sql = new SQL();

        setSELECT(sql);
        sql.FROM("district_tbl");
        if(distric != null){
            if(distric.getId() != null){
                sql.WHERE("id = #{id}");
            }
            if(distric.getCnName() != null){
                sql.WHERE("cn_name = #{cnName}");
            }
            if(distric.getParentId() != null){
                sql.WHERE("parent_id = #{parentId}");
            }
            if(distric.getInitial() != null){
                sql.WHERE("initial = #{initial}");
            }
            if(distric.getInitials() != null){
                sql.WHERE("initials = #{initials}");
            }
            if(distric.getPinyin() != null){
                sql.WHERE("pinyin = #{pinyin}");
            }
            if(distric.getExtra() != null){
                sql.WHERE("extra = #{extra}");
            }
            if(distric.getSuffix() != null){
                sql.WHERE("suffix = #{suffix}");
            }
            if(distric.getCode() != null){
                sql.WHERE("code = #{code}");
            }
            if(distric.getAreaCode() != null){
                sql.WHERE("area_code = #{areaCode}");
            }
            if(distric.getLevel() != null){
                sql.WHERE("level = #{level}");
            }
        }

        return sql.toString();
    }

    public String getDistricById(){
        SQL sql = new SQL();

        setSELECT(sql);
        sql.FROM("district_tbl");
        sql.WHERE("id = #{id}");

        return sql.toString();
    }

    private void setSELECT(SQL sql){
        sql.SELECT("id");
        sql.SELECT("cn_name AS cnName");
        sql.SELECT("parent_id AS parentId");
        sql.SELECT("initial");
        sql.SELECT("initials");
        sql.SELECT("pinyin");
        sql.SELECT("extra");
        sql.SELECT("suffix");
        sql.SELECT("code");
        sql.SELECT("area_code AS areaCode");
        sql.SELECT("level");
        sql.SELECT("create_time AS createTime");
        sql.SELECT("modify_time AS modifyTime");
    }
}
